<<<<<<< HEAD
#include <QApplication>
#include"gamewindow.h"
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    GameWindow window;
    window.show();
    return a.exec();
}
=======
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QKeyEvent>
#include <QMessageBox>
#include <QTime>
#include <QRandomGenerator>
#include <QPainter>
#include <QFont>
#include <QBrush>
#include <QColor>

class Game2048 : public QMainWindow {
    Q_OBJECT

public:
    Game2048(QWidget *parent = nullptr) : QMainWindow(parent) {
        setWindowTitle("2048 Game - Qt版");
        resize(500, 600);
        
        initGame();
        setupUI();
    }

protected:
    void keyPressEvent(QKeyEvent *event) override {
        bool moved = false;
        
        switch(event->key()) {
            case Qt::Key_Up:
                moved = moveUp();
                break;
            case Qt::Key_Down:
                moved = moveDown();
                break;
            case Qt::Key_Left:
                moved = moveLeft();
                break;
            case Qt::Key_Right:
                moved = moveRight();
                break;
            default:
                QMainWindow::keyPressEvent(event);
                return;
        }
        
        if (moved) {
            addRandomTile();
            updateScore();
            update();
            
            if (isGameOver()) {
                QMessageBox::information(this, "游戏结束", "游戏结束! 你的分数: " + QString::number(score));
                initGame();
                update();
            }
        }
    }

private slots:
    void newGame() {
        initGame();
        update();
    }

private:
    void initGame() {
        // 初始化游戏板
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                board[i][j] = 0;
            }
        }
        
        score = 0;
        addRandomTile();
        addRandomTile();
        updateScore();
    }

    void setupUI() {
        QWidget *centralWidget = new QWidget(this);
        QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
        
        // 分数显示
        QHBoxLayout *scoreLayout = new QHBoxLayout();
        scoreLabel = new QLabel("分数: 0", this);
        scoreLabel->setStyleSheet("QLabel { font-size: 20px; font-weight: bold; }");
        scoreLayout->addWidget(scoreLabel);
        
        QPushButton *newGameButton = new QPushButton("新游戏", this);
        connect(newGameButton, &QPushButton::clicked, this, &Game2048::newGame);
        scoreLayout->addWidget(newGameButton);
        
        mainLayout->addLayout(scoreLayout);
        
        // 游戏板
        gameBoard = new QLabel(this);
        gameBoard->setAlignment(Qt::AlignCenter);
        gameBoard->setMinimumSize(400, 400);
        gameBoard->setStyleSheet("QLabel { background-color: #bbada0; border-radius: 10px; }");
        mainLayout->addWidget(gameBoard, 1, Qt::AlignCenter);
        
        // 操作说明
        QLabel *instructions = new QLabel("使用方向键移动方块", this);
        instructions->setAlignment(Qt::AlignCenter);
        instructions->setStyleSheet("QLabel { font-size: 16px; }");
        mainLayout->addWidget(instructions);
        
        setCentralWidget(centralWidget);
    }

    void updateScore() {
        scoreLabel->setText("分数: " + QString::number(score));
    }

    void addRandomTile() {
        QList<QPoint> emptyCells;
        
        // 查找所有空格子
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                if (board[i][j] == 0) {
                    emptyCells.append(QPoint(i, j));
                }
            }
        }
        
        if (!emptyCells.isEmpty()) {
            // 随机选择一个空格子
            int index = QRandomGenerator::global()->bounded(emptyCells.size());
            QPoint pos = emptyCells.at(index);
            
            // 90%概率生成2，10%概率生成4
            board[pos.x()][pos.y()] = (QRandomGenerator::global()->bounded(10) < 9) ? 2 : 4;
        }
    }

    bool moveUp() {
        bool moved = false;
        
        for (int j = 0; j < 4; ++j) {
            for (int i = 1; i < 4; ++i) {
                if (board[i][j] != 0) {
                    int k = i;
                    while (k > 0 && board[k-1][j] == 0) {
                        board[k-1][j] = board[k][j];
                        board[k][j] = 0;
                        k--;
                        moved = true;
                    }
                    
                    if (k > 0 && board[k-1][j] == board[k][j]) {
                        board[k-1][j] *= 2;
                        score += board[k-1][j];
                        board[k][j] = 0;
                        moved = true;
                    }
                }
            }
        }
        
        return moved;
    }

    bool moveDown() {
        bool moved = false;
        
        for (int j = 0; j < 4; ++j) {
            for (int i = 2; i >= 0; --i) {
                if (board[i][j] != 0) {
                    int k = i;
                    while (k < 3 && board[k+1][j] == 0) {
                        board[k+1][j] = board[k][j];
                        board[k][j] = 0;
                        k++;
                        moved = true;
                    }
                    
                    if (k < 3 && board[k+1][j] == board[k][j]) {
                        board[k+1][j] *= 2;
                        score += board[k+1][j];
                        board[k][j] = 0;
                        moved = true;
                    }
                }
            }
        }
        
        return moved;
    }

    bool moveLeft() {
        bool moved = false;
        
        for (int i = 0; i < 4; ++i) {
            for (int j = 1; j < 4; ++j) {
                if (board[i][j] != 0) {
                    int k = j;
                    while (k > 0 && board[i][k-1] == 0) {
                        board[i][k-1] = board[i][k];
                        board[i][k] = 0;
                        k--;
                        moved = true;
                    }
                    
                    if (k > 0 && board[i][k-1] == board[i][k]) {
                        board[i][k-1] *= 2;
                        score += board[i][k-1];
                        board[i][k] = 0;
                        moved = true;
                    }
                }
            }
        }
        
        return moved;
    }

    bool moveRight() {
        bool moved = false;
        
        for (int i = 0; i < 4; ++i) {
            for (int j = 2; j >= 0; --j) {
                if (board[i][j] != 0) {
                    int k = j;
                    while (k < 3 && board[i][k+1] == 0) {
                        board[i][k+1] = board[i][k];
                        board[i][k] = 0;
                        k++;
                        moved = true;
                    }
                    
                    if (k < 3 && board[i][k+1] == board[i][k]) {
                        board[i][k+1] *= 2;
                        score += board[i][k+1];
                        board[i][k] = 0;
                        moved = true;
                    }
                }
            }
        }
        
        return moved;
    }

    bool isGameOver() {
        // 检查是否有空格子
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                if (board[i][j] == 0) {
                    return false;
                }
            }
        }
        
        // 检查是否有可以合并的相邻格子
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                if ((i < 3 && board[i][j] == board[i+1][j]) ||
                    (j < 3 && board[i][j] == board[i][j+1])) {
                    return false;
                }
            }
        }
        
        return true;
    }

    void paintEvent(QPaintEvent *event) override {
        QMainWindow::paintEvent(event);
        
        QPainter painter(gameBoard);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::TextAntialiasing);
        
        // 绘制游戏板背景
        painter.fillRect(gameBoard->rect(), QColor("#bbada0"));
        
        // 绘制格子
        int cellSize = 80;
        int margin = 10;
        int startX = (gameBoard->width() - (4 * cellSize + 3 * margin)) / 2;
        int startY = (gameBoard->height() - (4 * cellSize + 3 * margin)) / 2;
        
        QFont font;
        font.setBold(true);
        font.setPointSize(20);
        painter.setFont(font);
        
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                int x = startX + j * (cellSize + margin);
                int y = startY + i * (cellSize + margin);
                
                // 绘制格子背景
                QColor bgColor = getTileColor(board[i][j]);
                painter.setBrush(bgColor);
                painter.setPen(Qt::NoPen);
                painter.drawRoundedRect(x, y, cellSize, cellSize, 5, 5);
                
                // 绘制数字
                if (board[i][j] != 0) {
                    QColor textColor = (board[i][j] < 8) ? QColor("#776e65") : QColor("#f9f6f2");
                    painter.setPen(textColor);
                    
                    QString text = QString::number(board[i][j]);
                    QRect textRect(x, y, cellSize, cellSize);
                    painter.drawText(textRect, Qt::AlignCenter, text);
                }
            }
        }
    }

    QColor getTileColor(int value) {
        switch(value) {
            case 0:    return QColor("#cdc1b4");
            case 2:    return QColor("#eee4da");
            case 4:    return QColor("#ede0c8");
            case 8:    return QColor("#f2b179");
            case 16:   return QColor("#f59563");
            case 32:   return QColor("#f67c5f");
            case 64:   return QColor("#f65e3b");
            case 128:  return QColor("#edcf72");
            case 256:  return QColor("#edcc61");
            case 512:  return QColor("#edc850");
            case 1024: return QColor("#edc53f");
            case 2048: return QColor("#edc22e");
            default:   return QColor("#3c3a32");
        }
    }

    int board[4][4];
    int score;
    QLabel *scoreLabel;
    QLabel *gameBoard;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    
    Game2048 game;
    game.show();
    
    return app.exec();
}

#include "main.moc"
>>>>>>> edacdfcf1ccc8ddfdb01a20af8ba8373742e2d9b
